package moni.anyou.com.view.amap;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptor;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.maps2d.model.PolylineOptions;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import moni.anyou.com.view.R;
import moni.anyou.com.view.bean.InitTripDataBean;
import moni.anyou.com.view.bean.JingDianBean;
import moni.anyou.com.view.bean.request.ReqHomeBean;
import moni.anyou.com.view.utils.AppcationUtils;
import moni.anyou.com.view.utils.KJHttpBuild;

public class MapViewHelper {

    private MarkerOptions markerOption;
    private AMap aMap;
    private MapView mapView;
    private ArrayList<LatLng> points = new ArrayList<>();
    LatLng lastPoint;


    public MapViewHelper(MapView mapView) {
        this.mapView = mapView;
        if (aMap == null) {
            aMap = mapView.getMap();
        }
    }


    List<Marker> locationlist=new ArrayList<>();
    public void drawLocationUI(InitTripDataBean tripData) {

        points.clear();
        lastPoint = null;

        try {
            Bitmap bmp = BitmapFactory.decodeResource(mapView.getResources(), R.mipmap.ic_launcher);


            //获得路线的第一个点 并且将镜头移动到第一个点
            InitTripDataBean.Data.RoutePointList midPoint = tripData.list.get(0).routePointList.get((tripData.list.get(0).routePointList.size()-1)/2);
            LatLng fristLatLng = new LatLng(Double.valueOf(midPoint.latitude), Double.valueOf(midPoint.longitude));
            mapView.getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(fristLatLng, 12));

            points.clear();
            for (int i = 0, j = tripData.list.get(0).routePointList.size(); i < j; i++) {
                InitTripDataBean.Data.RoutePointList tempPoint = tripData.list.get(0).routePointList.get(i);
                LatLng point = new LatLng(Double.valueOf(tempPoint.latitude),Double.valueOf(tempPoint.longitude));
                points.add(point);


                markerOption =
                        new MarkerOptions()
                                .icon(BitmapDescriptorFactory.fromBitmap(bmp))
                                //                                .zIndex(0.13f)
                                .anchor(0,100)
                                .icon(BitmapDescriptorFactory
                                        .defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
                                //                                .icon(
                                //                                BitmapDescriptorFactory.defaultMarker(
                                ////                                    BitmapDescriptorFactory.HUE_AZURE)
                                //                                        BitmapDescriptorFactory.fromBitmap(bmp)
                                //                                ).anchor(0, 50)

                                .position(point);
//                                .draggable(true);
                aMap.addMarker(markerOption);


                    LatLng latlng1 = new LatLng(Double.valueOf(point.latitude), Double.valueOf(point.longitude));


                    Marker marker1 = aMap.addMarker(new MarkerOptions()
                            .position(latlng1)
                            .icon(BitmapDescriptorFactory
                                    .defaultMarker(BitmapDescriptorFactory.HUE_RED))
                            .draggable(false));



                locationlist.add(marker1);





//                markerOption =
//                        new MarkerOptions()
//                                .anchor(0, 50)
//                                //                                .anchor(0,50)
//                                //                                .icon(
//                                //                                BitmapDescriptorFactory.defaultMarker(
//                                ////                                    BitmapDescriptorFactory.HUE_AZURE)
//                                //                                        BitmapDescriptorFactory.fromBitmap(bmp)
//                                //                                ).anchor(0, 50)
//
//                                .position(point)
//                                .draggable(true);
//                aMap.addMarker(markerOption);

                if (lastPoint != null) {

                    aMap.addPolyline((new PolylineOptions()).add(lastPoint, point).color(Color.CYAN).setDottedLine(false));
                }
                lastPoint = point;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 绘制一个圆形
        //		      Circle circle = aMap.addCircle(new CircleOptions().center(point)
        //				.radius(4000).strokeColor(Color.argb(50, 1, 1, 1))
        //				.fillColor(Color.argb(20, 1, 1, 1)).strokeWidth(25));

    }


    public void drawLocationUI(JSONArray routePointList) {

        points.clear();
        lastPoint = null;

        try {
            Bitmap bmp = BitmapFactory.decodeResource(mapView.getResources(), R.mipmap.ic_launcher);
            //获得路线的第一个点 并且将镜头移动到第一个点
            JSONObject midPoint = (JSONObject) routePointList.get((routePointList.length()-1)/2);

            LatLng fristLatLng = new LatLng(midPoint.getDouble("latitude"), midPoint.getDouble("longitude"));
            mapView.getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(fristLatLng, 12));

            points.clear();
            for (int i = 0, j = routePointList.length(); i < j; i++) {
                JSONObject tempPoint = (JSONObject) routePointList.get(i);
                LatLng point = new LatLng(tempPoint.getDouble("latitude"), tempPoint.getDouble("longitude"));
                points.add(point);
                markerOption =
                        new MarkerOptions()
                                .icon(BitmapDescriptorFactory.fromBitmap(bmp))
                                //                                .zIndex(0.13f)
                                .anchor(0,100)
                                .icon(BitmapDescriptorFactory
                                        .defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
                                //                                .icon(
                                //                                BitmapDescriptorFactory.defaultMarker(
                                ////                                    BitmapDescriptorFactory.HUE_AZURE)
                                //                                        BitmapDescriptorFactory.fromBitmap(bmp)
                                //                                ).anchor(0, 50)

                                .position(point);
//                                .draggable(true);
                aMap.addMarker(markerOption);


                LatLng latlng1 = new LatLng(Double.valueOf(point.latitude), Double.valueOf(point.longitude));


                Marker marker1 = aMap.addMarker(new MarkerOptions()
                        .position(latlng1)
                        .icon(BitmapDescriptorFactory
                                .defaultMarker(BitmapDescriptorFactory.HUE_RED))
                        .draggable(false));



                locationlist.add(marker1);





//                markerOption =
//                        new MarkerOptions()
//                                .anchor(0, 50)
//                                //                                .anchor(0,50)
//                                //                                .icon(
//                                //                                BitmapDescriptorFactory.defaultMarker(
//                                ////                                    BitmapDescriptorFactory.HUE_AZURE)
//                                //                                        BitmapDescriptorFactory.fromBitmap(bmp)
//                                //                                ).anchor(0, 50)
//
//                                .position(point)
//                                .draggable(true);
//                aMap.addMarker(markerOption);

                if (lastPoint != null) {

                    aMap.addPolyline((new PolylineOptions()).add(lastPoint, point).color(Color.CYAN).setDottedLine(false));
                }
                lastPoint = point;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 绘制一个圆形
        //		      Circle circle = aMap.addCircle(new CircleOptions().center(point)
        //				.radius(4000).strokeColor(Color.argb(50, 1, 1, 1))
        //				.fillColor(Color.argb(20, 1, 1, 1)).strokeWidth(25));

    }



    List<Marker> markerslist=new ArrayList<>();
    public void showJingDian(InitTripDataBean tripData) {

        aMap = mapView.getMap();



        new KJHttpBuild().setRequestBean(ReqHomeBean.create(19).keyValue("teamid",tripData.list.get(0).teamInfo.get(0).teamID))
                .build(new KJHttpBuild.Listen<JingDianBean>() {

                    @Override
                    public void onSuccessData(String data, JingDianBean jsonObj) {
                        if (jsonObj.list.size()>0){

                            LatLng fristLatLng = new LatLng(Double.valueOf(jsonObj.list.get(0).latitude), Double.valueOf(jsonObj.list.get(0).longitude));
                            mapView.getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(fristLatLng, 12));


                            for (JingDianBean.Data bean : jsonObj.list) {

//                            bean.latitude="30.6000938736";
//                            bean.longitude="120.1904296875";
                                /**
                                 * 绘制系统默认的1种marker背景图片
                                 */
                                LatLng latlng1 = new LatLng(Double.valueOf(bean.latitude), Double.valueOf(bean.longitude));


                                Marker marker1 = aMap.addMarker(new MarkerOptions()
                                        .position(latlng1)
                                        .title(bean.routeScenicSpotName)
                                        .icon(BitmapDescriptorFactory
                                                .defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
                                        .draggable(false));


//                            BitmapDescriptor markerIcon = BitmapDescriptorFactory
//                                    .fromView(null);

                                marker1.showInfoWindow();
                                markerslist.add(marker1);
                            }
                        }
//                        for(JingDianBean.Data bean:jsonObj.list)
//                        {
//                            /**
//                             * 绘制系统默认的1种marker背景图片
//                             */
//                            LatLng latlng = new LatLng(Double.valueOf(bean.latitude+0.1), Double.valueOf(bean.longitude+0.2));
//                            Marker marker = aMap.addMarker(new MarkerOptions()
//                                    .position(latlng)
//                                    .title(bean.routeScenicSpotName)
//                                    .icon(BitmapDescriptorFactory
//                                            .defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
//                                    .draggable(true));
//
//                            marker.showInfoWindow();// 设置默认显示一个infowinfow
//                            markerslist.add(marker);
//
//                        }

                    }

                    @Override
                    public void onFailure(Throwable t, int errorNo, String strMsg) {
                        Toast.makeText(AppcationUtils.getInstance(),"err:"+t.toString(),Toast.LENGTH_LONG).show();
                    }
                });




    }
    public void clearJingDian() {
        for(Marker marker:markerslist)
        {
            marker.hideInfoWindow();
            marker.destroy();
        }
        markerslist.clear();
    }

    public void clearLocationUI() {
        for(Marker marker:locationlist)
        {
            marker.hideInfoWindow();
            marker.destroy();
        }
        points.clear();
        aMap.clear();
    }

    public void drawTuandui(InitTripDataBean tripData) {

    }


//    {
//        "cmd": "30",
//            "result": "1",
//            "totalCount": "1",
//            "list": [{
//        "routePointList": [{
//            "routePointID": "1",
//                    "routePointFlag": "1",
//                    "routePointName": "绩溪",
//                    "longitude": "118.578519",
//                    "latitude": "30.067533",
//                    "routeID": "1",
//                    "addTime": "2018-09-17 12:00:00",
//                    "routePointDetail": "",
//                    "teamID": "0"
//        }, {
//            "routePointID": "5",
//                    "routePointFlag": "4",
//                    "routePointName": "江南第一关",
//                    "longitude": "118.768215",
//                    "latitude": "30.134884",
//                    "routeID": "1",
//                    "addTime": "2018-09-17 12:00:00",
//                    "routePointDetail": "",
//                    "teamID": "0"
//        }, {
//            "routePointID": "7",
//                    "routePointFlag": "6",
//                    "routePointName": "黄茅培村",
//                    "longitude": "118.786911",
//                    "latitude": "30.136074",
//                    "routeID": "1",
//                    "addTime": "2018-09-17 12:00:00",
//                    "routePointDetail": "",
//                    "teamID": "0"
//        }, {
//            "routePointID": "8",
//                    "routePointFlag": "7",
//                    "routePointName": "下雪堂",
//                    "longitude": "118.813822",
//                    "latitude": "30.133786",
//                    "routeID": "1",
//                    "addTime": "2018-09-17 12:00:00",
//                    "routePointDetail": "",
//                    "teamID": "0"
//        }, {
//            "routePointID": "9",
//                    "routePointFlag": "8",
//                    "routePointName": "上雪堂",
//                    "longitude": "118.822373",
//                    "latitude": "30.13454",
//                    "routeID": "1",
//                    "addTime": "2018-09-17 12:00:00",
//                    "routePointDetail": "",
//                    "teamID": "0"
//        }, {
//            "routePointID": "11",
//                    "routePointFlag": "9",
//                    "routePointName": "蓝天凹",
//                    "longitude": "118.828434",
//                    "latitude": "30.134138",
//                    "routeID": "1",
//                    "addTime": "2018-09-17 12:00:00",
//                    "routePointDetail": "",
//                    "teamID": "0"
//        }, {
//            "routePointID": "12",
//                    "routePointFlag": "10",
//                    "routePointName": "永来村",
//                    "longitude": "118.851488",
//                    "latitude": "30.146877",
//                    "routeID": "1",
//                    "addTime": "2018-09-17 12:00:00",
//                    "routePointDetail": "",
//                    "teamID": "0"
//        }],
//        "teamInfo": [{
//            "teamID": "5",
//                    "teamName": "测试组队2",
//                    "userID": "294",
//                    "addTime": "2018-12-10 19:10:11",
//                    "teamPic": "route/20181206170604_940.jpg",
//                    "startTime": "2018-02-10",
//                    "endTime": "2018-11-10",
//                    "routeID": "1",
//                    "startFlag": "0",
//                    "status": "1",
//                    "equipment": "Sfsfsfsdf",
//                    "activityStrength": "adadasddasdasdasd",
//                    "activityFee": "100",
//                    "remark": "Sfdsdfsdfds",
//                    "disclaimer": "Fsdfsdfds",
//                    "contact": "1834242324",
//                    "scheduling": "Adsdadas",
//                    "recommendFlag": "0",
//                    "memberNumber": "0",
//                    "nick": "测试领队2",
//                    "routeName": "徽杭古道"
//        }],
//        "teamMemberList": [{
//            "teamMemberID": "5",
//                    "userID": "294",
//                    "addTime": "2018-12-08 22:15:54",
//                    "status": "1",
//                    "teamID": "5",
//                    "nick": "测试领队2",
//                    "icon": "292_1544357308729.png",
//                    "isLeader": "1",
//                    "routeName": "徽杭古道"
//        }, {
//            "teamMemberID": "6",
//                    "userID": "293",
//                    "addTime": "2018-12-08 22:15:54",
//                    "status": "1",
//                    "teamID": "5",
//                    "nick": "测试用户",
//                    "icon": "https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=3985062340,2188325304&fm=58",
//                    "isLeader": "0",
//                    "routeName": "徽杭古道"
//        }, {
//            "teamMemberID": "8",
//                    "userID": "292",
//                    "addTime": "2018-12-22 14:52:00",
//                    "status": "0",
//                    "teamID": "5",
//                    "nick": "测试领队",
//                    "icon": "292_1544357308729.png",
//                    "isLeader": "0",
//                    "routeName": "徽杭古道"
//        }, {
//            "teamMemberID": "12",
//                    "userID": "292",
//                    "addTime": "2018-12-23 19:45:01",
//                    "status": "0",
//                    "teamID": "5",
//                    "nick": "测试领队",
//                    "icon": "292_1544357308729.png",
//                    "isLeader": "0",
//                    "routeName": "徽杭古道"
//        }],
//        "recordList": []
//    }]
//    }
}
